package com.library.dao;

import com.library.bean.ReaderInfo;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Repository
public class ReaderInfoDao {

    @Resource
    public SqlSessionTemplate sqlSessionTemplate;

    private final static String namespace = "com.library.dao.ReaderInfoDao.";



    public long addReaderInfo(ReaderInfo readerInfo) {
        //插入成功返回大于0的值
        if (sqlSessionTemplate.insert(namespace+"addReaderInfo",readerInfo)>0){
            return sqlSessionTemplate.selectOne(namespace+"getReaderId",readerInfo);

        }else {
            return -1;
        }

    }

    public ArrayList<ReaderInfo> getAllReaderInfo() {
        List<ReaderInfo> result=sqlSessionTemplate.selectList(namespace+"getAllReaderInfo");
        return (ArrayList<ReaderInfo>) result;
    }

    public ReaderInfo getReaderInfo(long readerId) {
        return sqlSessionTemplate.selectOne(namespace+"getReaderInfo",readerId);
    }

    public int editReaderInfo(ReaderInfo readerInfo) {
        return sqlSessionTemplate.update(namespace+"editReaderInfo",readerInfo);
    }

    public int deleteReaderInfo(long readerId) {
        return sqlSessionTemplate.delete(namespace+"deleteReaderInfo",readerId);
    }
}
